#!/bin/sh

if [ $# -ne 1 ]; then 
	echo "usage: $0 configfile"
	exit -1
fi

# read config files

source $1

dir=$basedir$url

cd $bookdir

# 如果只有hash内容变化，也需要更新。

find . -mnewer timetag |grep -e $tag -e 'hash'  > tmp.list

if [ $? -ne 0 ];then
	echo "All is ok!"
	rm -f tmp.list
	exit 0
fi

TMPFILE=`echo $0|awk -F/ '{print $NF}'``date|sed -e "s/ //g"`.tmp.md

returnvalue=0

#sed -e "s/.*$tag//" tmp.list |sed -e "s/_.*//" > tmp.list.1

#mv tmp.list.1 tmp.list

# 删除文件中的hash文件，因为hash文件用genhash5处理。

sed -e "s/\.\///g" tmp.list | sed -e "/^hash/d" >tmp.list.1

mv tmp.list.1 tmp.list

#TMPFILE=`echo $0|awk -F/ '{print $NF}'``date|sed -e "s/ //g"`.tmp

bookname=`echo $bookname|sed -e "s/[1234567890]//g"`

for files in `sort tmp.list`

do

#	t_date=`find . -mnewer timetag |grep $tag$files |head -1| sed -e "s/.*_//" |sed -e "s/.md//"`

	if [ $? -eq 0 ]; then 


		chaptername=`grep "^#### " $files|sed -e "s/#### //g"`

		no=`echo $files |sed -e "s/$tag//g"|sed -e "s/_.*//g"`

#		echo "% "$bookname$files"-"$t_date >tmp.md

#		echo "% "$bookname""$no"章:"$chaptername 

		echo "% "$bookname":"$chaptername >tmp.md
		echo "% hbaolong@vip.qq.com" >>tmp.md
		commcode bookheader>>tmp.md
		echo "" >>tmp.md


		echored $no

		cat $files ~/workshop/hbaolong.github.io/etc/addchat.md >>tmp.md

#		cat `find . | grep $tag$files|head -1` ~/workshop/hbaolong.github.io/etc/addchat.md >>tmp.md
		pandoc -s  tmp.md -o $tag$no.html 
		((returnvalue=returnvalue+1))
	fi

done
#pandoc -s  tmp.md -o $tag$files.html 

# echo "生成名句文件"

getmjbook
#getmmbook
genhash5

## touch mingju.md memo.md

if [ -e mingju.md ];then 

	echo "% "$bookname "精彩瞬间" >tmp.md
	echo "% hbaolong@vip.qq.com" >>tmp.md

	commcode header >>tmp.md 
	commcode bookheader >> tmp.md
	echo "" >>tmp.md

	cat mingju.md  ~/workshop/hbaolong.github.io/etc/addchat.md >>tmp.md

	pandoc -s tmp.md -o mingju.html 
else
	touch mingju.md
fi


# echo "生成注解文件"
if [ -e memo.md ]; then 

	echo "% "$bookname "注解" >tmp.md
	echo "% hbaolong@vip.qq.com" >>tmp.md
	commcode header >>tmp.md 
	commcode bookheader >> tmp.md
	echo "" >>tmp.md

	cat memo.md  ~/workshop/hbaolong.github.io/etc/addchat.md >>tmp.md

	pandoc -s tmp.md -o memo.html 
else
	touch mingju.md
fi 

pandoc -s index.md -o index.html

procs="rich readcal books words"

if [ -e index_rich.md ]; then 
	echo "% "$bookname "丰富目录" >tmp.md
	echo "% hbaolong@vip.qq.com" >>tmp.md
	commcode header >>tmp.md 
	commcode bookheader >> tmp.md
	echo "" >>tmp.md

	cat index_rich.md  >>tmp.md
	pandoc -s tmp.md  -o index_rich.html
fi

if [ -e index_readcal.md ]; then 
	echo "% "$bookname "阅读日历" >tmp.md
	echo "% hbaolong@vip.qq.com" >>tmp.md
	commcode header >>tmp.md 
	commcode bookheader >> tmp.md
	echo "" >>tmp.md

	cat index_readcal.md  >>tmp.md
	pandoc -s tmp.md  -o index_readcal.html
fi

if [ -e index_books.md ]; then 
	echo "% "$bookname "引用书籍" >tmp.md
	echo "% hbaolong@vip.qq.com" >>tmp.md
	commcode header >>tmp.md 
	commcode bookheader >> tmp.md
	echo "" >>tmp.md

	cat index_books.md  >>tmp.md
	pandoc -s tmp.md  -o index_books.html
fi

if [ -e wordcounter.md ]; then 
	echo "% "$bookname "用字频率" >tmp.md
	echo "% hbaolong@vip.qq.com" >>tmp.md
	commcode header >>tmp.md 
	commcode bookheader >> tmp.md
	echo "" >>tmp.md

	cat wordcounter.md  >>tmp.md
	pandoc -s tmp.md  -o index_words.html
fi

if [ ! -d $dir ]; then 

	mkdir $dir

fi

mv *.html $dir

mv *.jpg $dir 2>/dev/null

touch timetag

rm -f tmp.*

#rm -f tmp.md

exit $returnvalue
